/*
* role.js
* create time:2019-07-05 09:00
* author: LiJunYi
* */
layui.extend({
   dtree:'/static/layui_ext/dtree/dtree'
}).use(['dtree','layer','element','form','table'],function(){
    var dtree = layui.dtree
        ,layer = layui.layer // 弹层
        , table = layui.table //表格
        , form = layui.form
        , element = layui.element;

    /* 数据表格 */
    table.render({
        elem: '#roleInfos'
        ,height: 400
        ,cellMinWidth:80
        ,url: '/role/list'
        , title: '角色表'
        , even: true
        , defaultToolbar: []
        , cols: [
            [
                {type: 'radio', fixed: 'left'}
                ,{field: 'id', title: '角色编号',align: 'center'}
                , {field: 'roleName', title: '角色名称', align: 'center'}
                , {field: 'roleSymbol', title: '权限字符', align: 'center'}
                ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:200}
            ]
        ]
        , page: true
        ,limits:[5,10,20,40,100]
    });

    //监听行工具事件
    table.on('tool(role_operation)', function(obj){
        var data = obj.data;
        //console.log(obj)
        if(obj.event === 'del'){
            layer.confirm('真的删除行么', function(index){
                obj.del();
                layer.close(index);
            });
        } else if(obj.event === 'edit'){
            layer.msg("编辑");
        }else if(obj.event === 'permission'){
            permissionlicense(data.id);
        }
    });

    /*解决表格行工具栏按钮过多展开失效start*/
    // 缓存当前操作的是哪个表格的哪个tr的哪个td
    $(document).off('mousedown','.layui-table-grid-down')
        .on('mousedown','.layui-table-grid-down',function(event){
            table._tableTrCurr = $(this).closest('td');
        });

    $(document).off('click','.layui-table-tips-main [lay-event]')
        .on('click','.layui-table-tips-main [lay-event]',function(event){
            var elem = $(this);
            var tableTrCurr = table._tableTrCurr;
            if(!tableTrCurr){
                return;
            }
            var layerIndex = elem.closest('.layui-table-tips').attr('times');
            // 关闭当前这个显示更多的tip
            layer.close(layerIndex);
            table._tableTrCurr.find('[lay-event="' + elem.attr('lay-event') + '"]').first().click();
        });
    /*解决表格行工具栏按钮过多展开失效end*/

    /*菜单许可*/
    function permissionlicense(roleId){
        layer.open({
           type:1,
           title:"菜单权限",
           area:['418px','88%'],
           content:'<ul id="openTree3" class="dtree" data-id="0"></ul>',
            btn:['确认赋权','关闭'],
            success:function(layero,index){
                var DTree = dtree.render({
                   obj:$(layero).find("#openTree3"),
                   url:"/role/permission/"+roleId,
                    dataFormat: "list",  //配置data的风格为list
                   checkbar:true  //开启复选框
                });
            },
            yes:function(index,layero){
                var params = dtree.getCheckbarNodesParam("openTree3"); // 获取选中值
                if(params.length == 0){
                    layer.msg("请至少选择一个节点",{icon:2});
                }
                var ids = [];
                for(var key in params){
                    var param = params[key];
                    ids.push(param.nodeId);
                }
                if(ids.length != 0){
                    $.ajax({
                        url:"/role/save_permission/" + roleId,
                        traditional:true,
                        type:"post",
                        data:{
                            permissionIds:ids
                        },
                        success:function(obj){
                            if("权限不足" == obj.msg){
                                layer.msg(obj.msg,{icon:7,time:800});
                            }else if(true == obj){
                                layer.msg("授权成功",{icon:1,time:800});
                            }else{
                                layer.msg("授权失败",{icon:2,time:800});
                            }
                            setTimeout(function () {
                                layer.close(index);
                            },900);
                        },
                        dataType:"json"
                    });
                }
            }
        });
    }
});
